;solution for euler 80

;from euler 65
(defun digital-sum (n)
	(let ((sum 0))
		(loop
			(when (= n 0) (return))
			(incf sum (mod n 10))
			(setf n (floor (/ n 10))))
		(return-from digital-sum sum)))

(defun babylon (S err)
	(let ((x (/ s 2)) nx)
		(loop
			(setf nx (/ (+ x (/ S x)) 2))
			(when (< (abs (- nx x)) err) (return))
			(setf x nx))
	(return-from babylon x)))
	
(defun solve-euler80 ()
	(let (root (err (expt 10 -100)) whole (totsum 0))
		(loop for i from 2 to 100 do
			(when (/= (sqrt i) (floor (sqrt i)))
				(setf root (babylon i err))
				(setf whole (floor (log (floor root) 10)))
				(setf root (floor (* root (expt 10 (- 99 whole)))))
				(incf totsum (digital-sum root))))
		(format t "Euler 80:~a" totsum)))
	